#include "HttpDownloadWidget.h"
#include "ui_HttpDownloadWidget.h"
#include "downloadbar.h"

#include <QFileInfo>
#include <QMenu>
#include <QFileDialog>
#include <QProgressBar>

HttpDownloadWidget::HttpDownloadWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::HttpDownloadWidget)
{
    ui->setupUi(this);
    ui->stackedWidget->setCurrentIndex(0);
    ui->fileListWidget->setFont(QFont("微软雅黑",12));
    //5.15.0: https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.15/5.15.0/submodules/qt3d-everywhere-src-5.15.0.tar.xz
    //5.15.1: https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.15/5.15.1/submodules/qt3d-everywhere-src-5.15.1.tar.xz
    //5.15.10:https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.15/5.15.10/submodules/qt3d-everywhere-opensource-src-5.15.10.tar.xz
    //5.15.11: https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.15/5.15.11/submodules/qt3d-everywhere-opensource-src-5.15.11.tar.xz
    ui->fileListWidget->addItem(new QListWidgetItem(QIcon(":/file_icon.png"),"qt3d-everywhere-src-5.15.0.tar.xz"));
    ui->fileListWidget->addItem(new QListWidgetItem(QIcon(":/file_icon.png"),"qt3d-everywhere-src-5.15.1.tar.xz"));
    ui->fileListWidget->addItem(new QListWidgetItem(QIcon(":/file_icon.png"),"qt3d-everywhere-opensource-src-5.15.10.tar.xz"));
    ui->fileListWidget->addItem(new QListWidgetItem(QIcon(":/file_icon.png"),"qt3d-everywhere-opensource-src-5.15.11.tar.xz"));
    connect(this,&HttpDownloadWidget::downloadTask,this,&HttpDownloadWidget::downloadFromFileListWidget);
}

HttpDownloadWidget::~HttpDownloadWidget()
{
    delete ui;
}

void HttpDownloadWidget::on_homeBtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}


void HttpDownloadWidget::on_downloadBtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void HttpDownloadWidget::contextMenuEvent(QContextMenuEvent *event)
{
    //通过鼠标能不能得到一个菜单项
    QPoint point = ui->fileListWidget->mapFromGlobal(event->globalPos());
    QListWidgetItem* item = ui->fileListWidget->itemAt(point);
    if(item){
        ui->fileListWidget->setCurrentItem(item);
        //创建菜单
        QMenu menu;
        QAction* action = menu.addAction("下载");
        connect(action,&QAction::triggered,[=](){
            //确定下载位置
            QString path = QFileDialog::getExistingDirectory(this,tr("请选择下载目录"),tr("D:\\"));
            if(path.isEmpty()) return;
            QUrl url;
            if(ui->fileListWidget->currentRow() == 0){
                url.setUrl("https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.15/5.15.0/submodules/qt3d-everywhere-src-5.15.0.tar.xz");
            }else if(ui->fileListWidget->currentRow() == 1){
                url.setUrl("https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.15/5.15.1/submodules/qt3d-everywhere-src-5.15.1.tar.xz");
            }else if(ui->fileListWidget->currentRow() == 2){
                url.setUrl("https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.15/5.15.10/submodules/qt3d-everywhere-opensource-src-5.15.10.tar.xz");
            }else if(ui->fileListWidget->currentRow() == 3){
                url.setUrl("https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.15/5.15.11/submodules/qt3d-everywhere-opensource-src-5.15.11.tar.xz");
            }
            if(url.isEmpty()) return;
            emit downloadTask(url,path+"/"+item->text());
        });
        menu.exec(event->globalPos());
    }
    QWidget::contextMenuEvent(event);
}

void HttpDownloadWidget::downloadFromFileListWidget(const QUrl& url, const QString& filePath)
{
    //添加进度条
    QListWidgetItem* item = new QListWidgetItem;
    item->setSizeHint(QSize(0,40));
    DownLoadBar* downloadBar = new DownLoadBar;
    QFileInfo info(filePath);
    QString fileName = info.fileName();
    if(fileName.size() >= 10){
        fileName = fileName.left(10);
        fileName += "...";
    }
    downloadBar->ui->fileNameLabel->setText(fileName);
    ui->downloadListWidget->addItem(item);
    ui->downloadListWidget->setItemWidget(item,downloadBar);

    http* task = new http(url,filePath);
    //进度条信号
    connect(task,&http::downloadProgress,qApp,[=](qint64 bytesReceived, qint64 bytesTotal){
        downloadBar->ui->progressBar->setRange(0,bytesTotal);
        downloadBar->ui->progressBar->setValue(bytesReceived);
    },Qt::QueuedConnection);

    //完成信号
    connect(task,&http::finished,qApp,[=](){
        ui->downloadListWidget->removeItemWidget(item);
        downloadBar->deleteLater();
        delete item;
    },Qt::QueuedConnection);
    QThreadPool::globalInstance()->start(task);
}

